<template>
  <!-- 收入统计 => 公司 -->
  <div class="pages financialFlow">
    <div class="pageLevel">
      <!-- 头部 -->
      <!-- 搜索框 -->
      <div class="total-income">
        <div class="all-income">
          <span> 预收款金额：{{prepaidFee}}元</span>
          <span style="margin-left:80px">预付款金额：{{prepayments}}元</span>
        </div>
      </div>
      <div class="tableSearch">
        <el-form label-width="100px" class="demo-ruleForm" size="small">
          <el-form-item label="交易时间:" style="margin-left:10px">
            <el-date-picker :clearable="false" format="yyyy-MM-dd" value-format="yyyy-MM-dd" v-model="value" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
              <!-- :picker-options="pickerOptions0" -->
            </el-date-picker>
          </el-form-item>
          <el-form-item label="财务类型:" style="margin-left:50px">
            <el-select v-model="selectdata.type" placeholder="请选择财务类型">
              <el-option label="全部" value=""></el-option>
              <el-option label="预收" value="1"></el-option>
              <el-option label="预付" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="交易类型:" style="margin-left:60px">
            <el-select v-model="selectdata.tradeType" placeholder="请选择交易类型">
              <el-option label="全部" value=""></el-option>
              <el-option label="预付款充值" value="PRE_RECHARGE"></el-option>
              <el-option label="预付款退款" value="PRE_RETURN"></el-option>
              <el-option label="商品采购" value="PRE_COMMODITY_PURCHASE"></el-option>
              <el-option label="商品退款" value="PRE_COMMODITY_RETURN"></el-option>
              <el-option label="初始值" value="PRE_INIT"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="wxser" class="wxelForm" label-width="55px">
            <el-button type="primary" @click="toPage" size="mini">搜索</el-button>
          </el-form-item>
          <el-form-item label="" label-width="35px">
            <el-button type="primary" v-if="setRole('导出')" icon="el-icon-download" size="mini" @click="download" class="Button">导出</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格 -->
      <div class="tableMain">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column type="index" label="序号" width="55" align="center"></el-table-column>
          <el-table-column prop="orderNo" label="订单号" align="center">
          </el-table-column>
          <el-table-column prop="createTime" label="交易时间" align="center">
            <!-- :formatter="formatTimes" -->
          </el-table-column>
          <el-table-column prop="operPrice" label="交易金额" align="center">
          </el-table-column>
          <el-table-column prop="financialType" label="财务类型" align="center">
          </el-table-column>
          <el-table-column prop="tradeType" label="交易类型" align="center" :formatter="tradeType">
          </el-table-column>
          <el-table-column prop="enterpriseName" label="企业名称" align="center">
          </el-table-column>
          <el-table-column prop="name" label="付款方/收款方" align="center">
          </el-table-column>
          <el-table-column prop="price" label="预收/预付款余额" align="center">
          </el-table-column>
        </el-table>
        <div class="pageblock">
          <el-button size="small" type="primary" class="topage" @click="toPage">确定</el-button>
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5,10,20,50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { accountingList, exportst, amountInquiry } from "@/api";
import util from "@/util";
import u from "@/util/state";
import { mapState } from "vuex";
var fileDownload = require("js-file-download");
export default {
  computed: mapState(["merchantId", "power"]),
  data() {
    return {
      pickerOptions0: {
        disabledDate(time) {
          return time.getTime() > Date.now() - 8.64e6; //如果没有后面的-8.64e6就是不可以选择今天的
        }
      },
      tradeType: u.tradeType,
      prepayments: "",
      prepaidFee: "",
      value: [],
      status: "",
      name: "",
      pageSize: 10,
      total: 0,
      currentPage: 1,
      selectdata: {
        tradeType: "",
        type: ""
      },
      tableData: [],
      options: [
        {
          name: "全部",
          id: ""
        },
        {
          name: "充值",
          id: "RECHARGE"
        },
        {
          name: "提现",
          id: "TASKCASH"
        },
        {
          name: "转入",
          id: "TRANSFER_IN"
        },
        {
          name: "转出",
          id: "TRANSFER_OUT"
        },
        {
          name: "pos收入",
          id: "B2CPAY"
        },
        {
          name: "批发收入",
          id: "B2BPAY_IN"
        },
        {
          name: "商品采购",
          id: "B2BPAY_OUT"
        },
        {
          name: "商品退款",
          id: "B2CRETURN"
        },
        {
          name: "批发退款",
          id: "B2BRETURN"
        },
        {
          name: "收款手续费",
          id: "B2CPAY_FEE"
        },
        {
          name: "提现手续费",
          id: "TASKCASH_FEE"
        },
        {
          name: "充值手续费",
          id: "RECHARGE_FEE"
        },
        {
          name: "汇款充值",
          id: "OFFLINE_TRANSFER"
        }
      ],
      sum: ""
    };
  },
  created() {
    this.getTime();
  },
  mounted() {
    this.getdata(10, 1);
    this.amountInquiry();
  },
  methods: {
    amountInquiry() {
      let postdata = {
        payeeId: this.merchantId
      };
      amountInquiry(postdata).then(res => {
        this.prepayments = res.data.prepayments;
        this.prepaidFee = res.data.prepaidFee;
      });
    },
    //配置权限
    setRole(key) {
      //console.log(this.power);
      return util.selectTitle(this.power, key);
    },
    //跳页
    toPage() {
      this.getdata(this.pageSize, 1);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getdata(val, this.currentPage);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getdata(this.pageSize, val);
    },
    getTime() {
      //---------------默认当前时间
      let date = new Date();
      let y = date.getFullYear();
      let m = date.getMonth() + 1;
      let d = date.getDate();
      if (m >= 1 && m <= 9) {
        m = "0" + m;
      }
      if (d >= 1 && d <= 9) {
        d = "0" + d;
      }
      let time = y + "-" + m + "-" + d;
      this.value[0] = time;
      this.value[1] = time;
    },
    formatTimes: function(row) {
      return util.foryear(row.createTime) + " " + util.fortime(row.createTime);
    },
    getdata(pageSize, pageNum) {
      if (!this.value) {
        this.value = ["", ""];
      }
      let postdata = {
        payeeId: this.merchantId,
        type: this.selectdata.type,
        tradeType: this.selectdata.tradeType,
        pageNo: pageNum,
        pageSize: pageSize,
        startTime: this.value[0],
        endTime: this.value[1]
      };
      accountingList(postdata).then(res => {
        this.tableData = res.data.list;
        this.total = res.data.total;
        this.status = res.data.status;
        this.name = res.data.name;
      });
    },
    //12.11新增导出功能
    download() {
      let datas = {
        state: 0,
        type: this.selectdata.type,
        payeeId: this.merchantId,
        tradeType: this.selectdata.selects,
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        startTime: this.value[0],
        endTime: this.value[1]
      };
      if (
        datas.endTime == "" ||
        datas.endTime == null ||
        datas.startTime == "" ||
        datas.startTime == null
      ) {
        this.$message({
          type: "error",
          message: "请检查导出条件是否完整"
        });
      }
      //console.log(this.tableData.length);
      if (this.tableData.length == 0) {
        this.$message({
          type: "error",
          message: "数据为空不能导出"
        });
      } else {
        exportst(datas)
          .then(res => {
            if (res.data == undefined) {
              let fileName = "账务流水.xlsx";
              fileDownload(res, fileName);
            } else {
              this.$message(res.data.msg);
            }
          })
          .catch(e => {});
      }
    }
  }
};
</script>
<style>
.financialFlow .el-input__inner {
  height: 32px;
  line-height: 32px;
}
#app {
  text-align: inherit;
}
.financialFlow .total-income {
  color: #606266;
  width: 100%;
  line-height: 55px;
  margin: 20px 0px 10px 0px;
  border-bottom: 1px solid #eee;
}
.financialFlow .total-income .all-income {
  margin-left: 30px;
}
.financialFlow .total-income .all-income span {
  margin-left: 10px;
}
.financialFlow .tableSearch .el-select {
  width: 140px;
}
.financialFlow .span1 {
  color: red;
  margin-left: 100px;
  font-size: 14px;
  font-weight: bold;
}
</style>
